package com.tuling.code.io.pseudoasync;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
/**
 * 使用线程池实现伪异步I/O
 * 所谓伪异步：底层仍然采用  “同步阻塞模型”
 * 主要弊端是在java的输入流：InutStream.read() 和 write();
 * 当对Socket的输入流进行读取操作的时候，它会一直阻塞下去，直到发生以下三种事件：
 * 		1、有数据可读
 * 		2、可用数据已经读取完毕
 * 		3、发生空指针异常或者I/O异常
 * @author javaw
 *
 */
public class TimeServer {
	public static void main(String[] args) {
		int port = 8080;
		ServerSocket server = null;
		try {
			server = new ServerSocket(port);
			System.out.println("The time server started!");
			Socket socket = null;
			
			TimeServerExecPoll singleExecutor = new TimeServerExecPoll(50,1000);
			while (true) {
				socket = server.accept();
				//伪异步实现点
				singleExecutor.execute(new TimeServerHandler(socket));
				
			}
			
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			if(null != null){
				System.out.println("Time server slosed!!");
				try {
					server.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
				server = null;
			}
		}
	}
}
